import {
    toast
} from 'nc-lightapp-front';
import {addPkorgRefer} from "../../../../../public/functions/addCondition";

let timer = null;

export default class HeaderAction {
    constructor(comp) {
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    /**
     * 人力资源组织改变
     * @param val
     */
    hrorgChange = async (val) => {
        const {props} = this.comp;
        const {transfer} = props;
        let meta = props.meta.getMeta();
        if (Object.keys(meta).length === 0) {
            await this.dispatch({
                type: 'transfer/update',
                payload: {
                    hrorgval: null,
                    hrorgobj: null,
                }
            });
            return
        }
        if(val.refpk === 'widget'){
            await this.dispatch({
                type:'transfer/update',
                payload:{
                    hrorgval: 'widget',
                    hrorgobj: {refpk:null},
                    widget: true,
                }
            });
            this.pubSub.publish('getWidgetTableData', {
                value: val.refpk
            });
            return
        }else{
            if (transfer.hrorgobj && transfer.hrorgobj.hasOwnProperty('refpk')) {
                if (val.refpk === transfer.hrorgobj.refpk) return;
            }
            let res = await this.dispatch({
                type: 'transfer/update',
                payload: {
                    hrorgval: val.refpk,
                    hrorgobj: val,
                    widget:false
                }
            });
            if (!res) return;
            if (transfer.init) {
                addPkorgRefer(props, {pk_org: val.refpk});
                this.pubSub.publish('getMainTableData', {
                    pk_org: val.refpk
                });
            }
        }

    };

    /**
     * 翻页组件 翻页
     * @param props
     * @param billid
     */
    pageChange = (props, billid) => {
        const {action} = this.comp;
        action.detailAct.queryDetailData({billid})
    };

    // 调配方式下拉框的内容数据
    getStapplyModeList = () => {
        const {props} = this.comp;
        const {transfer} = props;

        return [{
            value: '0',
            key: '', //
        }, {
            value: '1',
            key: transfer.language['gx6008-000088'], // '组织内调配'
        }, {
            value: '2',
            key: transfer.language['gx6008-000089'], // 调出
        }, {
            value: '3',
            key: transfer.language['gx6008-000090'],//   调入
        }];
    };

    // 选择调配方式
    changeStapplyMode = (value) => {
        if (value === '0') {
            value = null;
        }
        this.dispatch({
            type: 'transfer/update',
            payload: {
                stapplyMode: value
            }
        });
        if (this.comp.props.transfer.init) {
            this.pubSub.publish('getMainTableData', {
                stapplyMode: value
            });
        }
    };

    // 选择业务类型
    changeTrnsType = (value) => {
        this.dispatch({
            type: 'transfer/update',
            payload: {
                trnsType: value || null
            }
        });
        if (this.comp.props.transfer.init) {
            this.pubSub.publish('getMainTableData', {
                trnsType: value && value.refpk || null
            });
        }
    };

    // 订单状态下拉框的内容数据
    getOrderStatusList = () => {
        const {props} = this.comp;
        const {transfer} = props;

        return [{
            key: 'all',
            label: transfer.language['gx6008-000070'], // 全部
        }, {
            key: '-1',
            label: transfer.language['gx6008-000001'], // '自由态'
        }, {
            key: '3',
            label: transfer.language['gx6008-000002'], // 提交
        }, {
            key: '2',
            label: transfer.language['gx6008-000003'], // 审批进行中
        }, {
            key: '1',
            label: transfer.language['gx6008-000004'], // 审批通过
        }, {
            key: '0',
            label: transfer.language['gx6008-000005'], // 国际化处理： 审批不通过
        }, {
            key: '102',
            label: transfer.language['gx6008-000114'], // 已执行
        }];
    };

    // 订单时间范围的下拉框内容
    getOrderTimeRangeList = () => {
        const {props} = this.comp;
        const {transfer} = props;

        return {
            'oneWeek': transfer.language['gx6008-000012'], // 一周内
            'oneMonth': transfer.language['gx6008-000013'], // 一个月内
            'threeMonth': transfer.language['gx6008-000014'], // 三个月内
            'custom': transfer.language['gx6008-000015'], // 自定义
        };
    };

    // 选择订单状态
    changeOrderStatus = (value) => {
        let allIndex = value && value.indexOf('all');
        //如果返回的数据中 "all"放在第一个 且还有其他元素  说明是重新选择，则去掉"all"
        if (allIndex === 0 && value.length > 1) {
            value.splice(0, 1);
        } else if (allIndex > 0) {
            // 如果"all"存在，且不是第一位，则删掉其他的选项
            value = ["all"]
        }
        this.dispatch({
            type: 'transfer/update',
            payload: {
                orderStatus: value || ["all"]
            }
        });
        clearTimeout(timer);
        timer = setTimeout(() => {
            if (this.comp.props.transfer.init) {
                this.pubSub.publish('getMainTableData', {
                    billState: value || ["all"]
                });
            }
        }, 1000);
    };

    // 选择订单时间范围
    changeOrderRange = (value) => {
        let {props: {transfer}} = this.comp;
        this.dispatch({
            type: 'transfer/update',
            payload: {
                orderTimeRange: value
            }
        });

        if ((value !== 'custom' || transfer.beginTime) && transfer.init) {
            this.pubSub.publish('getMainTableData', {
                billTime: value
            });
        }
    };

    // 自定义订单时间范围
    changeOrderTime = (field) => {
        let {props: {transfer}} = this.comp;
        return (value) => {
            this.comp.setState({
                startEndTime: value
            });
            if (value.length === 0) {
                return
            }
            this.dispatch({
                type: 'transfer/update',
                payload: {
                    beginTime: value[0],
                    endTime: value[1]
                }
            });
            if (transfer.init) {
                this.pubSub.publish('getMainTableData', {
                    beginTime: value[0],
                    endTime: value[1]
                });
            }
        }
    }

}
